// =============================================================================
//  STATS - Statistical Analysis Tools, v.0.01
//  Copyright (C) 2012  Marco Vettigli
// -----------------------------------------------------------------------------
//
// This program  is free software:  you can  redistribute it and/or  modify it
// under the terms of the  GNU General Public License as published by the Free
// Software Foundation,  either version 3 of the License,  or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY;  without even  the  implied  warranty  of  MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License  for
// more details.
//
// You  should  have received a copy of the  GNU General Public License  along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// == module ===================================================================
//
// DIALOGCOLUMNPROPERTIES.CPP
// brief class description
//
// Author: Marco Vettigli
//
// TODO: write DialogColumnProperties module description
//
// == implementation ===========================================================
//
// TODO: write DialogColumnProperties implementation description
//
// =============================================================================

// == include directives =======================================================

#include "dialogcolumnproperties.h"
#include "ui_dialogcolumnproperties.h"

// == constructors and destructors =============================================

DialogColumnProperties::DialogColumnProperties(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogColumnProperties)
{
    ui->setupUi(this);
    ui->doubleLSL->setMinimum(-10);
    ui->doubleUSL->setMaximum(10);
}

DialogColumnProperties::~DialogColumnProperties()
{
    delete ui;
}


// == getters ==================================================================

const QString DialogColumnProperties::getColumnName() const
{
    return ui->lineName->text();
}

StatS::SColProperty::ArrayType DialogColumnProperties::getColumnType() const
{
    // based on current content combobox selection return SArrayType
    switch (ui->comboContent->currentIndex())
    {
    case 0: return StatS::SColProperty::SDoubles;
    case 1: return StatS::SColProperty::SStrings;
    default: break;
    }
    // else return undefined
    return StatS::SColProperty::Undefined;
}

QString DialogColumnProperties::getDescription() const
{
    return ui->textDescription->toPlainText();
}

double DialogColumnProperties::getLsl() const
{
    return ui->doubleLSL->value();
}

double DialogColumnProperties::getUsl() const
{
    return ui->doubleUSL->value();
}

QString DialogColumnProperties::getUnits() const
{
    return ui->lineUnits->text();
}

// == setters ==================================================================

bool DialogColumnProperties::setColumnName(const QString name)
{
    ui->lineName->setText(name);
    return true;
}

bool DialogColumnProperties::setColumnType(
    const StatS::SColProperty::ArrayType type)
{
    switch (type) {
    case StatS::SColProperty::SDoubles:
        ui->comboContent->setCurrentIndex(0);
        break;
    case StatS::SColProperty::SStrings:
        ui->comboContent->setCurrentIndex(1);
        break;
    case StatS::SColProperty::Undefined:
        ui->comboContent->setCurrentIndex(-1);
    default: return false;
    }
    return true;
}

bool DialogColumnProperties::setDescription(const QString description)
{
    ui->textDescription->setPlainText(description);
    return true;
}

bool DialogColumnProperties::setLsl(double lsl)
{
    ui->doubleLSL->setValue(lsl);
    return true;
}

bool DialogColumnProperties::setUsl(double usl)
{
    ui->doubleUSL->setValue(usl);
    return true;
}

bool DialogColumnProperties::setUnits(const QString units)
{
    ui->lineUnits->setText(units);
    return true;
}

// == inspectors ===============================================================

// == accessor function definitions ============================================

// == static variables =========================================================

// == static funtion definitions ===============================================

// == private function definitions =============================================

void DialogColumnProperties::on_buttonBox_accepted()
{
    this->accept();
}

void DialogColumnProperties::on_buttonBox_rejected()
{
    this->reject();
}

void DialogColumnProperties::on_comboContent_currentIndexChanged(int index)
{
    // based on current selected index, un/enable double spin boxes
    switch (index) {
    case 0:
    case -1:
        ui->doubleLSL->setValue(0);
        ui->doubleUSL->setValue(0);
        ui->doubleLSL->setDisabled(false);
        ui->doubleUSL->setDisabled(false);
        break;
    case 1:
        ui->doubleLSL->setDisabled(true);
        ui->doubleUSL->setDisabled(true);
        break;
    default: return;
    }
    return;
}

void DialogColumnProperties::on_doubleLSL_valueChanged(double value)
{
    // check if changed lsl is greater than usl and update usl
    if (value > ui->doubleUSL->value())
        ui->doubleUSL->setValue(value);
    // set minumum value of double spin boxes based on values magnitude
    ui->doubleUSL->setMinimum(value);
    if (value != 0 && value < 0) ui->doubleLSL->setMinimum(value*10);
    else ui->doubleLSL->setMinimum(-10);
    return;
}

void DialogColumnProperties::on_doubleUSL_valueChanged(double value)
{
    // check if changed usl is less than lsl and update lsl
    if (value < ui->doubleLSL->value())
        ui->doubleLSL->setValue(value);
    // set maximum value of double spin boxes based on values magnitude
    ui->doubleLSL->setMaximum(value);
    if (value != 0 && value > 0) ui->doubleUSL->setMaximum(value*10);
    else ui->doubleUSL->setMaximum(10);
    return;
}
